﻿<?php
if(!empty($_POST)){
	require_once("classes/mustang.php");
	require_once("classes/developer.php");
	require_once("classes/game.php");
	$mustangDB = new MustangDB();
	if(isset($_POST["game_name"]) && !empty($_POST["game_name"])){
		$game_name = $_POST["game_name"];
		if(Game::exist($game_name)){
			$game_name_error = "同名游戏已存在";
		}
	}else{
		$game_name_error = "游戏名称不能为空";
	}
	if(isset($_POST["os"]) && !empty($_POST["os"])){
		$os = $_POST["os"];
		if(count($os) > 1){
			$os_str = $os[0] . " " . $os[1];
		}else{
			$os_str = $os[0];
		}
	}else{
		$os_error = "请至少选择一个系统";
	}
	if(isset($_POST["company"])&& !empty($_POST["company"])){
		$company = $_POST["company"];
	}else{
		$company_error = "公司不能为空";
	}
	if(isset($_POST["email"])&& !empty($_POST["email"])){
		$email = $_POST["email"];
		if(Developer::exist($email)){
			$email_error = "邮箱已注册";
		}
	}else{
		$email_error = "邮箱不能为空";
	}
	if(isset($_POST["password"])&& !empty($_POST["password"])){
		$password = $_POST["password"];
		if(strlen($password) < 6){
			$password_error = "密码长度少于6";
		}elseif(strlen($password) > 20){
			$password_error = "密码长度大于20";
		}
	}else{
		$password_error = "密码不能为空";
	}
	if(isset($_POST["password_confirm"])&& !empty($_POST["password_confirm"])){
		$password_confirm = $_POST["password_confirm"];
		if(strcmp($password_confirm, $password)){
			$password_confirm_error = "确认密码与密码不匹配";
		}
	}else{
		$password_confirm_error = "确认密码不能为空";
	}
	
	if(isset($game_name_error) || isset($os_error) || isset($company_error) 
			|| isset($email_error) || isset($password_error) || isset($password_confirm_error)){
		$error_msg = "<ul class='errors'>";
		if(isset($game_name_error)){
			$error_msg .= "<li>$game_name_error</li>";
		}
		if(isset($os_error)){
			$error_msg .= "<li>$os_error</li>";
		}
		if(isset($company_error)){
			$error_msg .= "<li>$company_error</li>";
		}
		if(isset($email_error)){
			$error_msg .= "<li>$email_error</li>";
		}
		if(isset($password_error)){
			$error_msg .= "<li>$password_error</li>";
		}
		if(isset($password_confirm_error)){
			$error_msg .= "<li>$password_confirm_error</li>";
		}
		$error_msg .= "</ul>";
	}else{
		$dev = new Developer($mustangDB);
		$dev->initNew($email, $company, $password);
		$dev_id = $dev->save();
		$game = new Game($game_name, $os_str, $dev_id);
		$game->save();
		require_once("classes/mymail.php");
		$mail = new MyMail();
		$res = $mail->send($email, $dev->getToken());
		if($res){
			$success_msg = "注册成功！请查收邮件激活帐号</body>";
		}else{
			$error_msg = "发送验证邮件失败";
		}
	}
}
?>
<html>
<head>
<title>开发者注册</title>
<meta http-equiv="charset" content="utf-8">
<style type="text/css">
body{
	//text-align: center;
}
ul.errors{
	list-style-type: circle;
	list-style-position: inside;
	color: red;
	text-align: left;
}
ul.form{
	list-style-type: none;
	width: 800px;
}
ul.form li{
	width: 500px;
	height: 40px;
}
ul.form li label{
    display: inline-block;
	width: 200px;
	text-align: right;
	padding-right: 20px;
}
</style>
</head>
<body>
<?php 
	if(isset($error_msg)){
		echo $error_msg;
	}elseif(isset($success_msg)){
		echo $success_msg;
		exit;
	}
?>
<div>开发者注册</div>
<form action="#" method="post">
<ul class="form">
<li><label>游戏名称</label>
	<input name="game_name" type="text" value="<?php if(isset($game_name)){echo $game_name;} ?>"/></li>
<li><label>系统</label>
	<input name="os[]" type="checkbox" value="android" <?php if(isset($os) && in_array('android', $os)){echo "checked";}?> />Android
	<input name="os[]" type="checkbox" value="ios" <?php if(isset($os) && in_array('ios', $os)){echo "checked";}?>/>iOS</li>
<li><label>公司</label><input name="company" type="text" value="<?php if(isset($company)){echo $company;} ?>"/></li>
<li><label>邮箱</label><input name="email" type="text" value="<?php if(isset($company)){echo $email;} ?>"/></li>
<li><label>密码</label><input name="password" type="password" /></li>
<li><label>确认密码</label><input name="password_confirm" type="password" /></li>
<li><label></label><input name="submit" type="submit" value="注册"/></li>
</ul>
</form>
</body>
</html>